package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjAddProjectBean;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.ProjectReceiptBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.ProjectReceiptMapper;
import com.koron.css2.newProjectCenter.service.PjInfoService;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 补录工程款
 * @author xusheng
 *
 */
public class PjAdjustAdd implements ServerInterface {
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		try {
			PjAddProjectBean bean = JsonUtils.objectToPojo(req.getData(), PjAddProjectBean.class);
			//查询用水安装
			PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
			PjInfoBean pjInfoBean = pjInfoMapper.selectById(bean.getId());
			if (pjInfoBean == null) {
				return MessageBean.create(Constant.NOT_NULL, "用水工程单据不存在。", null);
			}
			pjInfoBean.setBudgetMoney(bean.getAddMoney());
			pjInfoBean.setDepositMoney(0.0);
			double accmoney = (bean.getAddMoney()==null?0.0:bean.getAddMoney());
			addProjectReceipt(factory, userInfo, pjInfoBean,"1", bean);
			pjInfoBean.setAccruedMoney(pjInfoBean.getAccruedMoney()==null?0.0:pjInfoBean.getAccruedMoney()+accmoney);
			//增加用水安装累计收款金额
			pjInfoMapper.saveAccruedMoney(pjInfoBean);
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
		} catch (Exception e) {
			factory.close(false);
			logger.error("工程款调整保存失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
		}
	}
	
	public void addProjectReceipt(SessionFactory factory, UserInfoBean userInfo,PjInfoBean pjInfoBean, String chargeType, PjAddProjectBean bean) {
			
			//组装工程收款单
			ProjectReceiptBean projectReceiptBean = new ProjectReceiptBean();
			projectReceiptBean.setId(Tools.getObjectId());
			projectReceiptBean.setBillId(pjInfoBean.getId());
			projectReceiptBean.setBillNo(pjInfoBean.getBillNo());
			projectReceiptBean.setBillDate(CommonUtils.getCurrentDate());
			projectReceiptBean.setBillType(PjInfoService.PROJECT_INFO_NEW_RECEIPT);
			projectReceiptBean.setReceivableMoney(pjInfoBean.getBudgetMoney());//应收
			projectReceiptBean.setProceedsMoney(pjInfoBean.getBudgetMoney());
			projectReceiptBean.setDepositMoney(pjInfoBean.getDepositMoney());//押金
			double accmoney = (pjInfoBean.getBudgetMoney()==null?0.0:pjInfoBean.getBudgetMoney())+(pjInfoBean.getDepositMoney()==null?0.0:pjInfoBean.getDepositMoney());
			projectReceiptBean.setTotalMoney(accmoney);
			projectReceiptBean.setChargeFlag(1);
			projectReceiptBean.setReturnFlag(1);
			projectReceiptBean.setPayWay(bean.getSetmtPayWay());
			projectReceiptBean.setComments("工程款调整单据编号：" + pjInfoBean.getBillNo() + "备注：" + bean.getComments());
			projectReceiptBean.setTenantId(userInfo.getCurWaterCode());
			projectReceiptBean.setCreateAccount(userInfo.getUserInfo().getAcount());
			projectReceiptBean.setCreateName(userInfo.getUserInfo().getName());
			projectReceiptBean.setChargeType(chargeType);
			
			//新增工程收款单
			ProjectReceiptMapper projectReceiptMapper = factory.getMapper(ProjectReceiptMapper.class);
			projectReceiptMapper.addNew(projectReceiptBean);
		}
}
